package com.wandian.games.mvvm.lottery;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;

import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Route;

import com.mylib.libcore.AppContext;
import com.mylib.libcore.config.CoinTypeEnum;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.tools.CSJADTools;
import com.mylib.libcore.tools.TTAdManagerHolder;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.games.R;
import com.wandian.games.databinding.ActivityLotteryBinding;
import com.wandian.games.dialog.LotteryPrizeDialog;
import com.wandian.games.views.CusLuckyView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;

import static com.mylib.libcore.config.AppConfig.CSJ_BANNER_ONE_ID;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 抽奖
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_GAMES_LOTTERY)
public class LotteryActivity extends BaseMVVMActivity<LotteryActivityViewModel> implements CusLuckyView.LotteryStart {
    private ActivityLotteryBinding dataBinding;
    //自定义-抽奖弹框
    private LotteryPrizeDialog lotteryPrizeDiaolog;
    private List<String> listBean = new ArrayList<>();//奖品
    private int sys_width = 375;

    private TTAdNative mTTAdNative;
    private TTRewardVideoAd mttRewardVideoAd;
    //看视频
    private boolean videoPrepare = false;
    private int  moneyNum = 1;
    //剩余抽奖次数
    private int lottery_num =0;

    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_lottery);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        //绘制不可抽奖
      //  dataBinding.luckyViewCus.drawDJSRect();
        initViewData();
        initClink();
        initCSJAD();
        try {
            lottery_num = Integer.parseInt(getIntent().getStringExtra("lottery_num")+"");
        }catch (Exception e){
            lottery_num = 0;
        }
        //获取广告
        CSJADTools.loadCSJAdMiddleBanner(getContext(),dataBinding.rlAdBottom,"",CSJ_BANNER_ONE_ID,375,120);
        dataBinding.tvLotteryNumber.setText(lottery_num+"");
    }

    /**
     * 初始化穿山甲广告
     */
    private void initCSJAD(){
        try{
            TTAdManagerHolder.init(this);
            mTTAdNative = TTAdManagerHolder.getTTAdNative(this);
            Random random = new Random();
            moneyNum = 1+random.nextInt(10);
            mTTAdNative.loadRewardVideoAd(CSJADTools.getAdSlot("抽奖奖励",moneyNum,"lottery"),
                    new TTAdNative.RewardVideoAdListener() {
                        @Override
                        public void onError(int i, String s) {
                            videoPrepare = false;
                        }

                        @Override
                        public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
                            videoPrepare = false;
                            if(UIUtils.isEmpty(ttRewardVideoAd))return;
                            mttRewardVideoAd = ttRewardVideoAd;
                        }

                        @Override
                        public void onRewardVideoCached() {
                            videoPrepare = true;
                        }
                    });
        }catch (Exception e){

        }
    }


    /**
     * 准备视频
     */
    private void prepareVideo(){
        if (mttRewardVideoAd != null && videoPrepare) {
            mttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {

                @Override
                public void onAdShow() {
                    Log.d("VideoAd","onAdShow");
                    //   Toast.makeText(RewardVideoActivity.this, "rewardVideoAd show", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onAdVideoBarClick() {
                    Log.d("VideoAd","onAdVideoBarClick");
                    //  Toast.makeText(RewardVideoActivity.this, "rewardVideoAd bar click", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onAdClose() {
                    Log.d("VideoAd","onAdClose");
                    //   Toast.makeText(RewardVideoActivity.this, "rewardVideoAd close", Toast.LENGTH_SHORT).show();
                    UIUtils.setKeyEveryDayCoin(CoinTypeEnum.LOTTERY_COIN);
                    mttRewardVideoAd = null;
                    lottery_num= lottery_num-1;
                    dataBinding.tvLotteryNumber.setText(lottery_num+"");
                    drawNumber();
                  //  CustomCoinToast.showToast(LotteryActivity.this,"抽奖奖励",num+"金币");
                }

                @Override
                public void onVideoComplete() {
                    Log.d("VideoAd","onVideoComplete");
                    // Toast.makeText(RewardVideoActivity.this, "rewardVideoAd complete", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onVideoError() {
                    Log.d("VideoAd","onVideoError");
                }

                @Override
                public void onRewardVerify(boolean b, int i, String s, int i1, String s1) {

                }


                @Override
                public void onSkippedVideo() {
                    Log.d("VideoAd","onSkippedVideo");
                }
            });
            //展示广告，并传入广告展示的场景
            mttRewardVideoAd.showRewardVideoAd(LotteryActivity.this, TTAdConstant.RitScenes.CUSTOMIZE_SCENES, "scenes_test");
        } else {
            ToastUtil.show("视频资源准备中，请稍后再试");
        }
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("在线抽奖");
        for(int i=0;i<9;i++){
            listBean.add((i+1)+"金币");
        }
        dataBinding.luckyViewCus.initLuckyView(listBean);
    }

    private void drawNumber(){//抽奖
        Random random =new Random();
        dataBinding.luckyViewCus.setLuckNum(random.nextInt(8));
        dataBinding.luckyViewCus.startAnim();
        initCSJAD();
    }

    private void initClink(){
        dataBinding.luckyViewCus.setLuckAnimationEndListener(new CusLuckyView.OnLuckAnimationEndListener() {
            @Override
            public void onLuckAnimationEnd(int pos, String msg) {
                showResultDialog((1+moneyNum/10f)+"");
                dataBinding.luckyViewCus.drawDJSRect();
                //打印抽奖结果
            }
        });

        dataBinding.luckyViewCus.setLotteryStartListener(this::setOnLotteryStart);

        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> finish());

    }

    //展示弹框
    private void showResultDialog(String goldcoin){
        try {
           sys_width =  AppContext.getInstance().getAppPreferenceForever().getAppSysWidth()-70;
           if(sys_width<=0)sys_width = 375;
            lotteryPrizeDiaolog = new LotteryPrizeDialog(sys_width,goldcoin,LotteryActivity.this);
            //显示窗口
            //设置layout在PopupWindow中显示的位置
            if(!UIUtils.isEmpty(dataBinding.llLottery)){
                lotteryPrizeDiaolog.showAtLocation(dataBinding.llLottery, Gravity.CENTER, 0, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void setOnLotteryStart() {
        if(lottery_num>0){
            prepareVideo();
        }else {
            ToastUtil.show("暂无抽奖次数，请浏览app增加次数");
        }
    }



}
